package net.cctms.zhishu.webAppFramework.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import net.cctms.zhishu.R;
import net.cctms.zhishu.webAppFramework.ui.view.SharePopupWindow;


/**
 * 分享弹出菜单
 *
 * @author yf on 2017/8/8
 */
public class ShareItemAdapter extends BaseAdapter {
    private Context context;
    private final static int SHARE_ITEM_NUM = 6;
    //private boolean isShareActivity = false;
    private boolean isInVisibleSms = false;
    private LayoutInflater layoutInflater;

    public ShareItemAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setIsInVisibleSms(boolean isInVisibleSms){
        this.isInVisibleSms = isInVisibleSms;
    }

    @Override
    public int getCount() {
        return SHARE_ITEM_NUM;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SharePopupWindowViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new SharePopupWindowViewHolder();
            convertView = layoutInflater.inflate(R.layout.share_popup_window_item,null);
            viewHolder.shareNameIv = (TextView)convertView.findViewById( R.id.share_popup_window_item_name_tv);
            viewHolder.rootView = convertView.findViewById(R.id.share_popup_window_item_root);
            viewHolder.shareIconIv = (ImageView)convertView.findViewById(R.id.share_popup_window_item_pic_iv);
           /* viewHolder.itemDivide = convertView.findViewById(R.id.share_popup_window_item_divide);*/
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (SharePopupWindowViewHolder)convertView.getTag();
        }
        handleItemView(viewHolder,position);
        return convertView;
    }

    private void handleItemView(SharePopupWindowViewHolder viewHolder, int position) {
        if(position >=3 ){
            viewHolder.itemDivide.setVisibility(View.VISIBLE);
        }else{
            viewHolder.itemDivide.setVisibility(View.INVISIBLE);
        }
        switch (position) {
            case SharePopupWindow.WEIXIN_FRIENDS:
                viewHolder.shareIconIv.setImageResource(R.mipmap.share_weixin_friends);
                viewHolder.shareNameIv.setText(context.getResources().getString(R.string.share_weixin_friends));
                break;
            case SharePopupWindow.WEXIN:
                viewHolder.shareIconIv.setImageResource(R.mipmap.share_weixin);
                viewHolder.shareNameIv.setText(context.getResources().getString(R.string.share_weixin));
                break;
            case SharePopupWindow.QZONE:
                viewHolder.shareIconIv.setImageResource(R.mipmap.share_qzone);
                viewHolder.shareNameIv.setText(context.getResources().getString(R.string.share_qq_zone));
                break;
            case SharePopupWindow.QQ:
                viewHolder.shareIconIv.setImageResource(R.mipmap.share_qq);
                viewHolder.shareNameIv.setText(context.getResources().getString(R.string.share_qq));
                break;
            case SharePopupWindow.WEIBO:
                viewHolder.shareIconIv.setImageResource(R.mipmap.share_weibo);
                viewHolder.shareNameIv.setText(context.getResources().getString(R.string.share_weibo));
                break;
            case SharePopupWindow.SMS:
                if(isInVisibleSms){
                    viewHolder.shareIconIv.setVisibility(View.INVISIBLE);
                    viewHolder.shareNameIv.setVisibility(View.INVISIBLE);
                    viewHolder.rootView.setBackgroundColor(context.getResources().getColor(R.color.transparent));
                }else{
                    viewHolder.shareIconIv.setImageResource(R.mipmap.share_message);
                    viewHolder.shareNameIv.setText(context.getResources().getString(R.string.share_sms));
                }
                break;
            default:
                break;
        }
    }

    public static class SharePopupWindowViewHolder {
        public View rootView;
        public ImageView shareIconIv;
        public TextView shareNameIv;
        public View itemDivide;
    }
}
